package com.example.twoeightgang;

import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class PaimingActivity extends Activity {
    private ListView listView_paiming;
    private List<HashMap<String,Object>> list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.paiming_layout);
        listView_paiming=(ListView)findViewById(R.id.listview_paihang);
        MainActivity mainActivity=new MainActivity();
        list=paiming();
        //Log.d("ykk","list1==" + list.get(2).get("numint"));
        if(list!=null) {
            listView_paiming.setAdapter(new SimpleAdapter(PaimingActivity.this, list, R.layout.listview_layout,
                    new String[]{"mingci", "namestring", "numint"}, new int[]{R.id.mingci, R.id.mingcixingming, R.id.mingcifenshu}));
        }else{
            Toast.makeText(PaimingActivity.this,getResources().getString(R.string.zanwupaiming),Toast.LENGTH_LONG);
        }
    }

    public List<HashMap<String,Object>> paiming(){
        CoinDateOpenHelper coinDateOpenHelper=new CoinDateOpenHelper(this,1);
        SQLiteDatabase coinDate= coinDateOpenHelper.getWritableDatabase();
        List<HashMap<String,Object>> paimingList=new ArrayList<HashMap<String,Object>>();
        Log.d("ykk","11111111111111111");
        Cursor cursor=coinDate.rawQuery("select * from playname order by num desc",null);
        //coinDate.execSQL("select * from playname order by 1");
        if(cursor!=null){
            //cursor.moveToFirst();
            int count = cursor.getCount();
            int i=1;
            MainActivity.totalnumber = count;
            Log.d("ykk","count == " + MainActivity.totalnumber);
            if(count>0) {
                while (cursor.moveToNext()) {
                        HashMap<String,Object> hashMap=new HashMap<String,Object>();
                        int a = cursor.getInt(cursor.getColumnIndex("num"));
                        String na = cursor.getString(cursor.getColumnIndex("name"));
                        Log.d("ykk", "aaaa == " + a + "  name == " + na);
                        hashMap.put("namestring", na);
                        hashMap.put("numint", a);
                        hashMap.put("mingci", "第" + (i++) + "名");
                        paimingList.add(hashMap);
                    }
                return paimingList;
            }
        }
        cursor.close();
        //Log.d("ykk","name 11 == " + paimingList.get(5).get("mingci") + "   num11== " + paimingList.get(5).get("numint"));
        return null;
    }
}
